package com.cloudlead.dictionaryAction.service.impl;
import com.cloudlead.common.lang.persistence.*;
import com.cloudlead.common.worker.client.exception.RestClientException;
import com.cloudlead.commonData.service.BaseCompanyService;
import com.cloudlead.commonData.service.TbContainerInfoService;
import com.cloudlead.commonData.service.TbCustomerOrderService;
import com.cloudlead.commonData.service.TbFleetOrderService;
import com.cloudlead.commonData.vo.*;
import com.cloudlead.dictionaryAction.service.AccountBillSer;
import com.cloudlead.dictionaryService.vo.AccountBillRebVo;
import com.cloudlead.dictionaryService.vo.AccountBillVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * Created by Will on 2016/1/12.
 */
@Service
public class AccountBillSerImpl implements AccountBillSer {
    @Autowired
    TbCustomerOrderService tbCustomerOrderService;
    @Autowired
    BaseCompanyService baseCompanyService;
    @Autowired
    TbFleetOrderService tbFleetOrderService;
    @Autowired
    TbContainerInfoService tbContainerInfoService;
    @Override
    public AccountBillRebVo findAllAccountBill(AccountBillVo accountBillVo,int rows,int page) {
        PageRequest pageRequest = new PageRequest();
        pageRequest.setPage(page);
        pageRequest.setSize(rows);
        Double dou = 0.0;
        Page page1 = null;
        List<Map> list2 = new ArrayList<Map>();//存放rows
        List<Map> list6 = new ArrayList<Map>();//存放footer
        List<Term> list = new ArrayList<Term>();
        AccountBillRebVo customerBillRebVo = new AccountBillRebVo();
        if(null != accountBillVo){
            if(null != accountBillVo.getCustomerName() && !"".equals(accountBillVo.getCustomerName())){
                if(null != accountBillVo.getForwardName() && !"".equals(accountBillVo.getForwardName())){
                    //货主跟货代对账
                    if(null != accountBillVo.getOrderBeginDate() && !"".equals(accountBillVo.getOrderBeginDate())){
                        list.add(new Term("ordertime",Operator.GT,accountBillVo.getOrderBeginDate()));
                    }
                    if(null != accountBillVo.getOrderEndDate() && !"".equals(accountBillVo.getOrderEndDate())){
                        list.add(new Term("ordertime",Operator.LT,accountBillVo.getOrderEndDate()));
                    }
                    list.add(new Term("totalForwarderPrice",Operator.NE,null));
                    list.add(new Term("baseCompany.id", accountBillVo.getCustomerName()));
                    list.add(new Term("belongForward.id", accountBillVo.getForwardName()));
                    Filter filter = new Filter(new Criteria(list));
                    List<TbCustomerOrder> list1 = tbCustomerOrderService.list(filter,null);
                    isNull(list1);

                    for(TbCustomerOrder tbCustomerOrder : list1){
                        dou+=tbCustomerOrder.getTotalForwarderPrice();
                    }
                    pageRequest.setFilter(filter);
                    page1=  tbCustomerOrderService.findAll(pageRequest);
                    //rows赋值
                    List<TbCustomerOrder> list3 =page1.getContent();
                    for(TbCustomerOrder tbCustomerOrder:list3){
                        Map map = new HashMap<>();
                        map.put("track",tbCustomerOrder.getBussinessCode());
                        map.put("containerInfo",tbCustomerOrder.getQuantity());
                        map.put("price",tbCustomerOrder.getTotalForwarderPrice());
                        map.put("time",tbCustomerOrder.getOrdertime());
                        list2.add(map);
                    }
                    //footer赋值
                    Map map = new HashMap<>();
                    map.put("track",list1.size());
                    map.put("containerInfo","");
                    map.put("price",dou);
                    list6.add(map);
                    //做数据
                    customerBillRebVo.setTotal(page1.getTotalPages());
                    customerBillRebVo.setRows(list2);
                    customerBillRebVo.setFooter(list6);
                    return customerBillRebVo;
                }else if(null != accountBillVo.getBaseFleetName() && !"".equals(accountBillVo.getBaseFleetName())){
                    //货主跟车队对账
                    //获取指定客户订单
                    TbCustomerOrder tbCustomerOrder = new TbCustomerOrder();
                    BaseCompany baseCompany = new BaseCompany();
                    baseCompany.setId(accountBillVo.getCustomerName());
                    tbCustomerOrder.setBaseCompany(baseCompany);
                    List<TbCustomerOrder> list4 = tbCustomerOrderService.list(tbCustomerOrder, null);
                    List list5 = new ArrayList<>();
                    for(TbCustomerOrder tbCustomerOrder1:list4){
                        list5.add(tbCustomerOrder1.getId());
                    }
                    list.add(new Term("fleetCompany.id", accountBillVo.getBaseFleetName()));
                    list.add(new Term("tbCustomerOrder.id", Operator.IN, list5));
                    list.add(new Term("price",Operator.NE,null));
                    if(null != accountBillVo.getOrderBeginDate() && !"".equals(accountBillVo.getOrderBeginDate())){
                        list.add(new Term("createTime",Operator.GT,accountBillVo.getOrderBeginDate()));
                    }
                    if(null != accountBillVo.getOrderEndDate() && !"".equals(accountBillVo.getOrderEndDate())){
                        list.add(new Term("createTime",Operator.LT,accountBillVo.getOrderEndDate()));
                    }
                    Filter filter = new Filter(new Criteria(list));
                    List<TbFleetOrder> list3 = tbFleetOrderService.list(filter, null);
                    pageRequest.setFilter(filter);
                    page1 =  tbFleetOrderService.findAll(pageRequest);
                    customerBillRebVo.setTotal(page1.getTotalPages());
                    List<TbFleetOrder> list1 = page1.getContent();
                    for(TbFleetOrder tbFleetOrder:list1){
                        Map map = new HashMap<>();
                        map.put("track",tbFleetOrder.getBussinessCode());
                        map.put("containerInfo",tbFleetOrder.getQuantity());
                        map.put("price",tbFleetOrder.getPrice());
                        map.put("time",tbFleetOrder.getCreateTime());
                        list2.add(map);
                    }
                    for(TbFleetOrder tbFleetOrder :list3){
                        dou+=tbFleetOrder.getPrice();
                    }
                    Map map = new HashMap<>();
                    map.put("track",list3.size());
                    map.put("containerInfo","");
                    map.put("price",dou);
                    list6.add(map);
                    customerBillRebVo.setRows(list2);
                    customerBillRebVo.setFooter(list6);
                    return customerBillRebVo;
                }else{
                    //货主账单
                    if(null != accountBillVo.getOrderBeginDate() && !"".equals(accountBillVo.getOrderBeginDate())){
                        list.add(new Term("ordertime",Operator.GT,accountBillVo.getOrderBeginDate()));
                    }
                    if(null != accountBillVo.getOrderEndDate() && !"".equals(accountBillVo.getOrderEndDate())){
                        list.add(new Term("ordertime",Operator.LT,accountBillVo.getOrderEndDate()));
                    }
                    list.add(new Term("baseCompany.id", accountBillVo.getCustomerName()));
                    list.add(new Term("totalPrice",Operator.NE,null));
                    Filter filter = new Filter(new Criteria(list));
                    List<TbCustomerOrder> list4 = tbCustomerOrderService.list(filter, null);
                    isNull(list4);
                    for(TbCustomerOrder tbCustomerOrder : list4){
                        dou+=tbCustomerOrder.getTotalPrice();
                    }
                    pageRequest.setFilter(filter);
                    page1=  tbCustomerOrderService.findAll(pageRequest);
                    //rows赋值
                    List<TbCustomerOrder> list3 =page1.getContent();
                    for(TbCustomerOrder tbCustomerOrder:list3){
                        Map map = new HashMap<>();
                        map.put("track",tbCustomerOrder.getBussinessCode());
                        map.put("containerInfo",tbCustomerOrder.getQuantity());
                        map.put("price",tbCustomerOrder.getTotalPrice());
                        map.put("time",tbCustomerOrder.getOrdertime());
                        list2.add(map);
                    }
                    //footer赋值
                    Map map = new HashMap<>();
                    map.put("track",list4.size());
                    map.put("containerInfo","");
                    map.put("price",dou);
                    list6.add(map);
                    //做数据
                    customerBillRebVo.setTotal(page1.getTotalPages());
                    customerBillRebVo.setRows(list2);
                    customerBillRebVo.setFooter(list6);
                    return customerBillRebVo;


                }
            }else if(null != accountBillVo.getForwardName() && !"".equals(accountBillVo.getForwardName())){
                  //货代账单
                if(null != accountBillVo.getOrderBeginDate() && !"".equals(accountBillVo.getOrderBeginDate())){
                    list.add(new Term("ordertime",Operator.GT,accountBillVo.getOrderBeginDate()));
                }
                if(null != accountBillVo.getOrderEndDate() && !"".equals(accountBillVo.getOrderEndDate())){
                    list.add(new Term("ordertime",Operator.LT,accountBillVo.getOrderEndDate()));
                }
                list.add(new Term("belongForward.id", accountBillVo.getForwardName()));
                list.add(new Term("totalForwarderPrice",Operator.NE,null));
                Filter filter = new Filter(new Criteria(list));
                List<TbCustomerOrder> list4 = tbCustomerOrderService.list(filter, null);
                isNull(list4);
                for(TbCustomerOrder tbCustomerOrder : list4){
                    dou+=tbCustomerOrder.getTotalForwarderPrice();
                }
                pageRequest.setFilter(filter);
                page1=  tbCustomerOrderService.findAll(pageRequest);
                //rows赋值
                List<TbCustomerOrder> list3 =page1.getContent();
                for(TbCustomerOrder tbCustomerOrder:list3){
                    Map map = new HashMap<>();
                    map.put("track",tbCustomerOrder.getBussinessCode());
                    map.put("containerInfo",tbCustomerOrder.getQuantity());
                    map.put("price",tbCustomerOrder.getTotalForwarderPrice());
                    map.put("time",tbCustomerOrder.getOrdertime());
                    list2.add(map);
                }
                //footer赋值
                Map map = new HashMap<>();
                map.put("track",list4.size());
                map.put("containerInfo","");
                map.put("price",dou);
                list6.add(map);
                //做数据
                customerBillRebVo.setTotal(page1.getTotalPages());
                customerBillRebVo.setRows(list2);
                customerBillRebVo.setFooter(list6);
                return customerBillRebVo;

            }else if(null !=accountBillVo.getBaseFleetName() && !"".equals(accountBillVo.getBaseFleetName())){
                //车队账单
                if(null != accountBillVo.getOrderBeginDate() && !"".equals(accountBillVo.getOrderBeginDate())){
                    list.add(new Term("createTime",Operator.GT,accountBillVo.getOrderBeginDate()));
                }
                if(null != accountBillVo.getOrderEndDate() && !"".equals(accountBillVo.getOrderEndDate())){
                    list.add(new Term("createTime",Operator.LT,accountBillVo.getOrderEndDate()));
                }
                list.add(new Term("fleetCompany.id", accountBillVo.getBaseFleetName()));
                list.add(new Term("price",Operator.NE,null));
                Filter filter = new Filter(new Criteria(list));
                List<TbFleetOrder> list3 = tbFleetOrderService.list(filter, null);
                isNull(list3);
                pageRequest.setFilter(filter);
                page1 =  tbFleetOrderService.findAll(pageRequest);
                customerBillRebVo.setTotal(page1.getTotalPages());
                List<TbFleetOrder> list1 = page1.getContent();
                for(TbFleetOrder tbFleetOrder:list1){
                    Map map = new HashMap<>();
                    map.put("track",tbFleetOrder.getBussinessCode());
                    map.put("containerInfo",tbFleetOrder.getQuantity());
                    map.put("price",tbFleetOrder.getPrice());
                    map.put("time",tbFleetOrder.getCreateTime());
                    list2.add(map);
                }
                for(TbFleetOrder tbFleetOrder :list3){
                    dou+=tbFleetOrder.getPrice();
                }
                Map map = new HashMap<>();
                map.put("track",list3.size());
                map.put("containerInfo","");
                map.put("price",dou);
                list6.add(map);
                customerBillRebVo.setRows(list2);
                customerBillRebVo.setFooter(list6);
                return customerBillRebVo;
            }
        }
        return null;
    }

    @Override
    public List<BaseCompany> findBaseCompany(String identity) {
        //0:车队，1：船东，2：货主，3:货代
        AccountBillRebVo customerBillRebVo = new AccountBillRebVo();
        BaseCompany baseCompany = new BaseCompany();
        baseCompany.setIdentity(identity);
        return  baseCompanyService.list(baseCompany,null);
    }
     String isNull(Object object){
         if(null==object){
             throw new RestClientException("没有对应账单");
         }
        return null;
    }
}
